<?php
/**
 * 用户基本资料等信息操作 业务层
 * @author yangchao
 * 包括用户登录，用户密码加密，用户cookie验证和加密，都在本业务逻辑定义
 * @package user
 * @copyright yangchao
 * @version 1.0
 * @code-encode utf-8
 * @data-encode 2010-01-17
 */

class user_info {
	
	/**
	 * 当前登录用户的ID
	 *
	 * @var int
	 */
	private $_cur_uid = "";
	
	private $secret_key = "yangchao";
	
	private $_cur_user_info = array();
		

	/**
	 *  根据UID取用户详细信息
	 * @param unknown_type $uid
	 */
	function get_user_info_by_uid($uid)
	{
		$db_user_info = new db_user_info();
		
		$user_detai_info = $db_user_info->get_user_info_item_by_uid($uid);
		
		return $user_detai_info;
	}
	/**
	 *  根据用户名和密码取用户详细信息
	 * @param unknown_type $uid
	 */
	function get_user_item_by_name_and_password($user_name,$password)
	{
		$db_user_info = new db_user_info();
		$md5_password = $this->encryption($password);
		$user_detai_info = $db_user_info->get_user_item_by_name($user_name);
		
		//check password
		if($user_detai_info["password"] == $md5_password)
		{
			return $user_detai_info;
		}
		else
		{
		return false;
		}
	}
	
	/**
	 * 密码加密方式
	 * @param unknown_type $password
	 */
	function encryption($password)
	{
		return md5($password);
	}
	
	/**
	 * 
	 * @param array $user_detail_info
	 */
	function set_cookie_by_user_info($user_detail_info)
	{
		
		$this->drop_cookie();
		
		$cookie_info = "uid={$user_detail_info["uid"]}&user_name={$user_detail_info["user_name"]}";
		
		$cookie_key = $this->cookie_encryption($cookie_info);
		
		setcookie("GINFO",$cookie_info, time()+3600*24,"/",COOKIE_DOMAIN);
		setcookie("GKEY",$cookie_key, time()+3600*24,"/",COOKIE_DOMAIN);
     
		//兼容xweibo		
		include_once($_SERVER['DOCUMENT_ROOT'].'/xweibo/application/adapter/account/xauthCookie_account.adp.php');
		$xwbAccount = new xauthCookie_account();
		$res = $xwbAccount->_setLocalToken(array('uid'=>$user_detail_info["uid"], 'uname'=>$user_detail_info["user_name"]));
		
		return true;
	}
	
	
	
	/**
	 * cookie加密方式
	 * @param unknown_type $password
	 */
	function cookie_encryption($cookie_info)
	{
		return md5($cookie_info).md5($this->secret_key);
	}
	
	/**
	 * 删除cookie信息
	 */
	function drop_cookie()
	{
	
		session_unset();
	
		setcookie("GINFO","", 0,"/",COOKIE_DOMAIN);
		setcookie("GKEY","",0,"/",COOKIE_DOMAIN);
		//兼容xweibo	
		include_once($_SERVER['DOCUMENT_ROOT'].'/xweibo/application/adapter/account/xauthCookie_account.adp.php');
		
		$xwbAccount = new xauthCookie_account();
		$xwbAccount->_setLocalToken(null);
		
		return true;
	}
	
	
	/**
	 * 判断是否已经登陆
	 */
	function is_logined()
	{
		
		if(!isset($_COOKIE["GKEY"]) || !isset($_COOKIE["GINFO"]))
		{
			return false;
		}
		
		$compare_key = $this->cookie_encryption($_COOKIE["GINFO"]);
	
		if($_COOKIE["GKEY"] ==  $compare_key)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 用户注册
	 * @param $user_name
	 * @param $password
	 */
	function insert_user_info($user_name,$password)
	{
		$en_password = $this->encryption($password);
		
		$db_user_info = new db_user_info();
		//check have exist
		$user_detai_info = $db_user_info->get_user_item_by_name($user_name);
		
		if($user_detai_info)
		{
			throw new Exception("用户已存在");
			
			return false;			
		}
		else
		{
			$insert_res = $db_user_info->insert_user_info($user_name,$en_password);
		}
		return $insert_res;
	}
	
	/**
	 * 获取当前登录用户信息
	 */
	function get_cur_user_info()
	{
		if($this->is_logined())
		{
			$uid = $this->get_cur_uid();	
			$user_detail_info = $this->get_user_info_by_uid($uid);	
			return $user_detail_info;		
		}
		else
		{
			return false;
		}
	
	}
	
	function get_cur_uid()
	{
		if(!$this->_cur_uid )
		{
			$cookie_info = $_COOKIE["GINFO"];
			parse_str($cookie_info);
			$this->_cur_uid = $uid;
		}	
		return $this->_cur_uid;
	}
	
}
	